package com.alpsoft.spotcheck.util;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.alpsoft.spotcheck.model.User;
import com.alpsot.spotcheck.activity.CheckClazz;
import com.alpsot.spotcheck.activity.DailyCheck;
import com.alpsot.spotcheck.activity.LoginActivity;
import com.alpsot.spotcheck.activity.WeekInspection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class Utils {
	
	/**
	 * 判断当前网络状态
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isConnectedNetwork(Context context) {
		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connManager.getActiveNetworkInfo() != null) {
			return connManager.getActiveNetworkInfo().isAvailable();
		}
		return false;
	}

	/**
	 * 杀死当前应用的进程
	 */
	public static void killProcess() {
		int pid = android.os.Process.myPid();
		android.os.Process.killProcess(pid);
	}

	/**
	 * 震动调用方法
	 */
	public static void vibratorContrl(final Context context){
		
	}
	
	/**
	 * 用户登录_扫卡
	 * 
	 * @param empid
	 */
	public static void login(final Context context, final Fragment fram,String cardNo,
			final String type) {
		// 登录
		HttpUtils http = new HttpUtils(5000);
		// String url = GlobalVar.httpUrl +
		// "login/app/validateLogin?cardNo="+"A55CC473";// cardNo;//"A55CC473";
		String url = GlobalVar.httpUrl + "login/app/validateLogin?cardNo="
				+ cardNo;
		System.out.println("url=" + url);
		http.send(HttpRequest.HttpMethod.GET, url,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						System.out.println("arg0=" + arg0);
						Message message = new Message();
						message.obj = "服务器连接异常";
						message.what = GlobalVar.msg_LoginFailure;
						if (type != null && type.equals("login")) {
							((LoginActivity) context).myHandler
									.sendMessage(message);
						}
						if (type != null && type.equals("CheckClazz")) {
							((CheckClazz)fram).myHandler
									.sendMessage(message);
						}
						if (type != null && type.equals("DailyCheck")) {
							((DailyCheck)fram).myHandler
							.sendMessage(message);
						}
						if (type != null && type.equals("WeekInspection")) {
							((WeekInspection)fram).myHandler
							.sendMessage(message);
						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String jsonStr = arg0.result;
						String result = "";
						String sessionId;
						String rows;
						Message message = new Message();
						message.what = GlobalVar.msg_LoginFailure;
						try {
							JSONObject obj = new JSONObject(jsonStr);
							result = obj.getString("result");
							sessionId = obj.getString("sessionId");
							rows = obj.getString("rows");
							message.obj = obj.getString("msg");
							if ("SUCCESS".equals(result)) {
								SharedPreferencesUtils.setSessionId(context,
										sessionId);
								rows = rows.replaceAll("\"\"", "null");
								// 保存用户信息
								GlobalVar.user = new Gson().fromJson(rows,
										new TypeToken<User>() {
										}.getType());
								SharedPreferencesUtils.setLoginInfo(context,
										GlobalVar.user);
								message.what = GlobalVar.msg_LoginSuccess;
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						if (type != null && type.equals("login")) {
							((LoginActivity) context).myHandler
									.sendMessage(message);
						}
						if (type != null && type.equals("CheckClazz")) {
							((CheckClazz)fram).myHandler
							.sendMessage(message);
						}
						if (type != null && type.equals("DailyCheck")) {
							((DailyCheck)fram).myHandler
							.sendMessage(message);
						}
						if (type != null && type.equals("WeekInspection")) {
							((WeekInspection)fram).myHandler
							.sendMessage(message);
						}
					}
				});
	}

	/**
	 * 用户登录_用户名密码
	 * 
	 * @param empid
	 */
	public static void loginByName(final Context context,final Fragment fram, String userName,
			String passWord, final String type) {
		// 登录
		HttpUtils http = new HttpUtils(5000);
		String url = GlobalVar.httpUrl
				+ "login/app/validateLogin_stocktake?userName=" + userName
				+ "&passWord=" + passWord;
		Log.i("ADFADFADFA账号密码的值为：", userName+"****"+passWord);
		http.send(HttpRequest.HttpMethod.GET, url,
				new RequestCallBack<String>() {
					@Override
					public void onFailure(HttpException arg0, String arg1) {
						System.out.println("arg0=" + arg0);
						Message message = new Message();
						message.obj = "服务器连接异常";
						message.what = GlobalVar.msg_LoginFailure;
						if (type != null && type.equals("login")) {
							((LoginActivity) context).myHandler
									.sendMessage(message);
						}
						if (type != null && type.equals("CheckClazz")) {
							((CheckClazz)fram).myHandler
							.sendMessage(message);
						}
						if (type != null && type.equals("DailyCheck")) {
							((DailyCheck)fram).myHandler
							.sendMessage(message);
						}
						if (type != null && type.equals("WeekInspection")) {
							((WeekInspection)fram).myHandler
							.sendMessage(message);
						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String jsonStr = arg0.result;
						String result = "";
						String sessionId;
						String rows;
						Message message = new Message();
						message.what = GlobalVar.msg_LoginFailure;
						try {
							JSONObject obj = new JSONObject(jsonStr);
							result = obj.getString("result");
							sessionId = obj.getString("sessionId");
							rows = obj.getString("rows");
							message.obj = obj.getString("msg");
							if ("SUCCESS".equals(result)) {
								SharedPreferencesUtils.setSessionId(context,
										sessionId);
								rows = rows.replaceAll("\"\"", "null");
								// 保存用户信息
								GlobalVar.user = new Gson().fromJson(rows,
										new TypeToken<User>() {
										}.getType());
								SharedPreferencesUtils.setLoginInfo(context,
										GlobalVar.user);
								message.what = GlobalVar.msg_LoginSuccess;
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						if (type != null && type.equals("login")) {
							((LoginActivity) context).myHandler
									.sendMessage(message);
						}
						if (type != null && type.equals("CheckClazz")) {
							((CheckClazz)fram).myHandler
							.sendMessage(message);
						}
						if (type != null && type.equals("DailyCheck")) {
							((DailyCheck)fram).myHandler
							.sendMessage(message);
						}
						if (type != null && type.equals("WeekInspection")) {
							((WeekInspection)fram).myHandler
							.sendMessage(message);
						}
					}
				});
	}

}
